//
//  RegisterUserInfoViewController.m
//  GYMWinners
//
//  Created by Logic Solutions on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterUserInfoViewController.h"
#import "RegisterRuleViewController.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"
#import "BaseroutAPI.h"

@interface RegisterUserInfoViewController ()

@end


@implementation RegisterUserInfoViewController

@synthesize userNameTxt;
@synthesize emailAddressTxt;
@synthesize passwordTxt;


-(IBAction)backgroundTouched:(id)sender{
    [userNameTxt resignFirstResponder];
    [emailAddressTxt resignFirstResponder];
    [passwordTxt resignFirstResponder];
}

-(IBAction)textfieldReturn:(id)sender{
    [sender resignFirstResponder];
}

-(IBAction)back {
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];

}

- (BOOL) validate
{
    if( [[BaseroutAPI sharedInstance] eMailValidation:emailAddressTxt.text] == NO )
    {
        [[BaseroutAPI sharedInstance] MessageBox:@"邮件地址错误" Message:@"请输入正确邮件地址"];
        return NO;
    }
    
    if( [passwordTxt.text length] < 1)
    {
        [[BaseroutAPI sharedInstance] MessageBox:@"密码错误" Message:@"请输入密码"];
        
        return NO;
    }
    
    if( [userNameTxt.text length] < 1 )
    {
        [[BaseroutAPI sharedInstance] MessageBox:@"用户名错误" Message:@"请输入用户名"];
        
        return NO;
    }
    
    return YES;
    
}

-(IBAction)nextStep{

    [userNameTxt resignFirstResponder];
    [emailAddressTxt resignFirstResponder];
    [passwordTxt resignFirstResponder];
    
    if([self validate] == NO)
        return;
    
    NSMutableDictionary *slist = [[NSMutableDictionary alloc] init];
    [slist setObject:emailAddressTxt.text forKey:@"email"];
    [slist setObject:userNameTxt.text forKey:@"username"];
    [slist setObject:passwordTxt.text forKey:@"password"];
    [self zSendServer:slist theTitle:@"正在注册" theCommand:@"/register/create"];

}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = newBackButton; 
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - ASIFormDataRequest delegate


- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (request.responseStatusCode == 200) {
        
        NSString *responseString = [request responseString];
        NSLog(@"responseString=[%@]", responseString);   
        NSDictionary *responseDict = [responseString JSONValue];
        NSString *customerID = [NSString stringWithFormat:@"%@", [responseDict objectForKey:@"id"]];
        NSString *status = [responseDict objectForKey:@"status"];
        
        if( [[BaseroutAPI sharedInstance] validateString:customerID] )
        {
            // Register OK!
            [[BaseroutAPI sharedInstance] zSetVar:@"customerid" theValue:[responseDict objectForKey:@"id"]];
            [[BaseroutAPI sharedInstance] zSetVar:@"password" theValue:[responseDict objectForKey:@"password"]];
            [[BaseroutAPI sharedInstance] zSetVar:@"username" theValue:[responseDict objectForKey:@"username"]];
            [[BaseroutAPI sharedInstance] zSetVar:@"email" theValue:[responseDict objectForKey:@"email"]];
            
            // Clean them in case logout
            emailAddressTxt.text = @"";
            userNameTxt.text = @"";
            passwordTxt.text = @"";
            
            
            RegisterRuleViewController* registerRule=[[RegisterRuleViewController alloc] init];
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.8];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
            
            [self.navigationController pushViewController:registerRule animated:YES];
            
            [UIView commitAnimations];
        }
        else if( [status caseInsensitiveCompare:@"UsernameOrEmailExists"] == NSOrderedSame )
        {
            [[BaseroutAPI sharedInstance] MessageBox:@"注册失败" Message:@"用户名或电子邮件已存在"];      
        }
        else
        {
            [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"注册失败"]; 
        }
        
    } else {
        NSLog(@"Unexpected error");  
        [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
    }
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSError *error = [request error];
    NSLog(@"Error=%@", error.localizedDescription);   
    
    [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
}

@end
